// pages/appointHistoryC/appointHistoryC.js
var util = require('../../utils/util');
let today = util.formatTime(new Date())
Page({
  data: {
    activeNum: 1,
    appoint: [],
    appointt: [],
    today: today,
    doctorId:''
  },
  onUnload(){
    wx.switchTab({
      url: '/pages/me/me',
    })
  },
  onLoad(options) {
    var that = this
    console.log(options.doctorId)
    that.setData({
      doctorId:options.doctorId
    })
    //拿医师id
    let doctorId = options.doctorId
    //根据医师id取得这个医师当天正在进行的所有预约信息
    this.takeNowAppoint(doctorId)
    //根据医师id取得这个医师的所有预约信息
    // this.takeDoctorAppoint(doctorId)
  },
  //获取正在预约信息
  takeNowAppoint(doctorId) {
    var that = this
    wx.cloud.callFunction({
      name: 'appoint',
      data: {
        action: 'findDoctorShow',
        doctorId: doctorId,
        appointDay: today,
        isPassed: false,
        isOver: false,
      }
    }).then(res => {
      console.log('获取这个医师的当天正在预约信息成功', res)
      that.setData({
        appointt: res.result.data,
      })
    }).catch(res => {
      console.log('获取这个医师的当天正在预约信息失败', res)
    })
  },
  //获取这个医生预约信息
  takeDoctorAppoint(doctorId) {
    var that = this
    wx.cloud.callFunction({
      name: 'appoint',
      data: {
        action: 'findDoctorNoDay',
        doctorId: doctorId,
      }
    }).then(res => {
      console.log('获取这个医师的所有预约信息成功', res)
      that.setData({
        appoint: res.result.data.reverse(),
      })
    }).catch(res => {
      console.log('获取这个医师的所有预约信息失败', res)
    })

  },
  bindtap1() {
    this.setData({
      activeNum: 1,
    })
    this.takeNowAppoint(this.data.doctorId)
  },
  bindtap2() {
    this.setData({
      activeNum: 2,
    })
    this.takeDoctorAppoint(this.data.doctorId)
  },
  bindtap3() {
    this.setData({
      activeNum: 3,
    })
    this.takeDoctorAppoint(this.data.doctorId)
  },
  startDiagnose(e) { //开始就诊
    console.log(e.currentTarget.dataset.flowid)
    console.log(e.currentTarget.dataset.doctorid)
    console.log(e.currentTarget.dataset.number)
    //改变当前医生的当前排号
    wx.cloud.callFunction({
      name: 'doctorInfo',
      data: {
        action: 'changeNowNumber',
        doctorId: e.currentTarget.dataset.doctorid,
        nowNumber: e.currentTarget.dataset.number,
      }
    }).then(res => {
      console.log('改变医师当前排号成功', res)
      //跳到就诊界面
      wx.navigateTo({
        url: '/pages/diagnose/diagnose?flowId=' + e.currentTarget.dataset.flowid,
      })
    }).catch(res => {
      console.log('改变医师当前排号失败', res)
    })
  },
  //取消预约
  del(e) {
    var that = this
    wx.cloud.callFunction({
      name: 'appoint',
      data: {
        action: 'changePasedByFlowId',
        flowId: e.currentTarget.dataset.flowid,
        isPassed: true
      }
    }).then(res => {
      console.log('取消预约成功', res)
      wx.showToast({
        title: '取消预约成功',
      })
      //刷新
      that.takeNowAppoint(e.currentTarget.dataset.doctorid)
      that.takeDoctorAppoint(e.currentTarget.dataset.doctorid)
    }).catch(res => {
      console.log('取消预约失败', res)
      wx.showToast({
        icon: "none",
        title: '取消预约失败，请检查网络',
      })
    })
  },
  //查看已完成的订单
  seeOrder(e){
    wx.navigateTo({
      url: '/pages/order/order?flowId='+e.currentTarget.dataset.flowid,
    })
  }
})